/*
 * S6B33B2.h
 *
 * Created: 12.07.2011 17:08:27
 *  Author: Sven Fabricius
 */ 

#ifndef S6B33B2_H_
#define S6B33B2_H_

#include <avr/io.h>
#include <stdio.h>
//#define F_CPU 8000000UL
#include <util/delay.h>

#define LCD_Data PORTC
#define LCD_Data_Control DDRC

#define LCD_Status PORTA
#define LCD_Status_Control DDRA
#define LCD_WRB 4
#define LCD_RDB 3
#define LCD_RS 2
#define LCD_CS1B 1
#define LCD_RSTB 0

#define DB0 0x01
#define DB1 0x02
#define DB2 0x04
#define DB3 0x08
#define DB4 0x10
#define DB5 0x20
#define DB6 0x40
#define DB7 0x80

#define NON_OPERATION 0x00

#define OSCILLATION_MODE_SET 0x02
#define OMS_OSC DB0
#define OMS_EXT DB1

#define DRIVER_OUTPUT_MODE_SET 0x10
#define DOMS_SWP DB1
#define DOMS_SDIR DB2
#define DOMS_DLN_144 DB4
#define DOMS_DLN_162 DB5
#define DOMS_DLN_92 (DB4 | DB5)

#define DC_DC_SELECT 0x20
#define DDS_DC1_X15 DB0
#define DDS_DC1_X20 DB1
#define DDS_DC1_X20a (DB0 | DB1)
#define DDS_DC2_X15 DB2
#define DDS_DC2_X20 DB3
#define DDS_DC2_X20a (DB2 | DB3)

#define BIAS_SET 0x22
#define BS_BIAS1_15 DB0
#define BS_BIAS1_16 DB1
#define BS_BIAS1_17 (DB0| DB1)
#define BS_BIAS2_15 DB4
#define BS_BIAS2_16 DB5
#define BS_BIAS2_17 (DB4| DB5)

#define DCDC_CLOCK_DIVISION_SET 0x24
#define DCDS_DIV1_8 DB0
#define DCDS_DIV1_16 DB1
#define DCDS_DIV1_32 (DB0 | DB1)
#define DCDS_DIV2_8 DB4
#define DCDS_DIV2_16 DB5
#define DCDS_DIV2_32 (DB4 | DB5)

#define DCDC_AND_AMP_ON_OFF_SET 0x26
#define DAAOOS_DCDC1 DB0
#define DAAOOS_DCDC2 DB1
#define DAAOOS_DCDC3 DB2
#define DAAOOS_AMP DB3

#define TEMPERATURE_COMPENSATION_SET 0x28
#define TCS_05 DB0
#define TCS_10 DB1
#define TCS_15 (DB0 | DB1)

#define CONTRAST_CONTROL1 0x2A

#define CONTRAST_CONTROL2 0x2B

#define STANDBY_MODE_OFF 0x2C

#define STANDBY_MODE_ON 0x2D

#define DDRAM_BURST_MODE_OFF 0x2E
#define DDRAM_BURST_MODE_ON 0x2F
#define DBMO_BM DB0

#define ADDRESSING_MODE_SET 0x30
#define AMS_SGM DB0
#define AMS_SGP1 DB1
#define AMS_SGP2 DB2
#define AMS_SGP4 (DB1 | DB2)
#define AMS_SGF DB3
#define AMS_DSG DB4
#define AMS_GSM4096 DB5
#define AMS_GSM256 DB6
#define AMS_GSM256a (DB5 | DB6)

#define ROW_VECTOR_MODE_SET 0x32
#define RVMS_VEC DB0
#define RVMS_INC2 DB1
#define RVMS_INC4 DB2
#define RVMS_INC8 (DB1 | DB2)
#define RVMS_INC16 DB3
#define RVMS_INC16a (DB1 | DB3)
#define RVMS_INC16b (DB2 | DB3)
#define RVMS_INCi (DB1 | DB2 | DB3)

#define N_LINE_INVERSION_SET 0x34
#define NLIS_FIP DB6
#define NLIS_FIM DB7

#define FRAME_FREQUENCY_CONTROL 0x36
#define FFC_LFS DB0

#define ENTRY_MODE_SET 0x40
#define EMS_RWM DB0
#define EMS_XY DB1
#define EMS_MDI DB2
#define EMS_HL DB3

#define X_ADDRESS_AREA_SET 0x42
#define Y_ADDRESS_AREA_SET 0x43
#define RAM_SKIP_AREA_SET 0x45

#define DISPLAY_OFF 0x50
#define DISPLAY_ON 0x51
#define SPECIFIED_DISPLAY_PATTERN_SET 0x53
#define PARTIAL_DISPLAY_MODE_SET 0x55
#define PARTIAL_DISPLAY_START_LINE_SET 0x56
#define PARTIAL_DISPLAY_END_LINE_SET 0x57
#define AREA_SCROLL_MODE_SET 0x59
#define SCROLL_START_LINE_SET 0x5A
#define SET_DISPLAY_DATA_LENGTH 0xFC
#define TEST_MODE1 0xFF
#define TEST_MODE2 0xFE
#define TEST_MODE3 0xFD
#define TEST_MODE4 0xFB
#define TEST_MODE5 0xFA
#define TEST_MODE6 0xF9
#define OTP_MODE_OFF 0xEA
#define OTP_MODE_ON 0xEB
#define OFFSET_VOLUME_SET 0xED
#define OTP_WRITE_ENABLE 0xEF

#define to_rgb565(r,g,b) (((r / 8) << 11) | ((g / 4) << 5) | (b / 8))
#define LCD_Status_High(LCD_Status_Flag) LCD_Status |= (1<<LCD_Status_Flag)
#define LCD_Status_Low(LCD_Status_Flag) LCD_Status &= ~(1<<LCD_Status_Flag)
#define LCD_WData(DATA) {\
 LCD_OutByteRS1((uint8_t)((DATA >> 8) & 0xFF));\
 LCD_OutByteRS1((uint8_t)(DATA & 0xFF));\
}

void LCD_OutByteRS0(uint8_t Data);
void LCD_OutByteRS1(uint8_t Data);
void LCD_WCom( int data, ... );
void LCD_BData(uint8_t dataH, uint8_t dataL);
void S6B33B2_INIT(uint16_t Color);
void Display_Test(void);
void fillRect(uint16_t Color, uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height);
void drawRect(uint16_t Color, uint8_t Line, uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height);
void putChar(uint16_t BackColor, uint16_t FrontColor, char C, uint8_t X, uint8_t Y);
void putText(uint16_t BackColor, uint16_t FrontColor, char *Text, uint8_t X, uint8_t Y);
void writeLn(char *Text);
void setFillArea(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height);
void setXY(int Xn, int Yn);
void setFrontColor(uint16_t Color);
void setBackColor(uint16_t Color);
void clear(void);
void pgm_read_block( uint8_t* pTarget, const uint8_t* pSource, size_t len );
#endif /* S6B33B2_H_ */